/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.QuestionnaireDAO;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.genericdao.GenericDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class QuestionnaireDAOImpl extends GenericDAO<Questionnaire> implements QuestionnaireDAO {

    public QuestionnaireDAOImpl() {
        super(Questionnaire.class);
    }

    @Override
    public List<Questionnaire> findQuestionnaire(Questionnaire questionnaire) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Questionnaire find(Questionnaire questionnaire) {
        String sql = "SELECT q FROM Questionnaire q WHERE"
                + " q.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", questionnaire.getId());
        return (Questionnaire) query.getSingleResult();
    }

    @Override
    public List<Questionnaire> findQuestionnaireStates() {
        String sql = "SELECT q FROM Questionnaire q WHERE"
                + " q.states = true";
        Query query = getEntityManager().createQuery(sql);
        return (List<Questionnaire>) query.getResultList();
    }

    @Override
    public List<Questionnaire> findQuestionnaire(Questionnaire questionnaire, Issues issues) {
        List<Questionnaire> lista = new ArrayList<Questionnaire>();

        String sql = "SELECT q FROM Questionnaire q ";


        if ((issues.getTitle() != null) && (issues.getTitle() != "")) {
            sql += " JOIN q.categorys c JOIN c.issueses i  ";
        }

        sql += " where";

        if ((questionnaire.getTitulo() != "") && (questionnaire.getTitulo() != null)) {
            sql += " lower(q.titulo) like lower(:titulo) and  ";
        }
        
        if (issues != null) {
            if ((issues.getTitle() != "") && (issues.getTitle() != null)) {
                sql += " lower(i.title) like lower(:title) and ";
            }
        }
       
        sql += " 1=1 ";
        System.err.println(sql);
        Query query = getEntityManager().createQuery(sql);

        if (issues != null) {
            if ((issues.getTitle() != "") && (issues.getTitle() != null)) {
                query.setParameter("title", "%" + issues.getTitle() + "%");
            }
        }
        
        if ((questionnaire.getTitulo() != "") && (questionnaire.getTitulo() != null)) {
            query.setParameter("titulo", "%" + questionnaire.getTitulo() + "%");
        }
        
        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }

    @Override
    public List<Questionnaire> autoCompleteQuestionnaire(Questionnaire questionnaire) {
        List<Questionnaire> lista = new ArrayList<Questionnaire>();
        String sql = "SELECT q FROM Questionnaire q ";
        
        if ((questionnaire.getTitulo() != "") && (questionnaire.getTitulo() != null)) {
            sql += " WHERE lower(q.titulo) like lower(:titulo) ";
        }   
        System.err.println(sql);
        Query query = getEntityManager().createQuery(sql);
        
        if ((questionnaire.getTitulo() != "") && (questionnaire.getTitulo() != null)) {
            query.setParameter("titulo", "%" + questionnaire.getTitulo() + "%");
        }
        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }
}
